home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!marnold
- From: marnold@netcom.com (Matt Arnold)
- Subject: Re: () syntax for constructor for an array?
- Message-ID: <marnoldDo18pM.L94@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- References: <NICKC.96Mar7121149@uxe.liv.ac.uk>
- Date: Sun, 10 Mar 1996 03:36:58 GMT
- Sender: marnold@netcom20.netcom.com
-
- nickc@liv.ac.uk (Spider plant breeding program) writes:
-
- >I can initalise an array with:
-
- >float f[2] = { 2.718, 3.142 };
-
-
- >Is there some form of C++ syntax that can be used in a constructor - ie:
-
-
- >class a
- >{
- > float f[2];
-
- >public:
- > a() : f( /* What goes in here ? */ )
- > {};
- >};
-
- There is no syntax file this. The closest you could get it is to use
- the fact that *structs* can be implicity copied to and from each other...
-
- struct float_array { float element[2]; }; // array inside a struct
-
- class a
- {
- static float_array init = { { 1.0, 2.0 } };
-
- float_array f;
-
- public:
- a() : f(init)
- {};
- };
-
- Course, inside class a, you will have to refer to the members of the
- array like this...
-
- f.element[index]
-
- ...a slight syntactic inconvenience.
-
- The other approach in the use the Standard Template Library (STL) class
- vector for your array of floats. Objects of type vector can be intialized
- with each other. You can even initialize a vector with a normal array of
- floats...
-
- class a
- {
- static float init = { 1.0, 2.0 };
-
- vector<float> f;
-
- public:
- a() : f(f_init, f_init + 2)
- {};
- };
-
- And, then you could go back to using...
-
- f[index]
-
-
- The last suggestion is to just stop worrying about intializing your array
- in the intializer list and simply copy the initial values in the ctor body
- (no STL or struct tricks either)...
-
- class a
- {
- float f[2];
-
- public:
- a() { f[0] = 1.0; f[1] = 2.0; };
- };
-
-
-
- Regards,
- -------------------------------------------------------------------------
- Matt Arnold | | ||| | |||| | | | || ||
- marnold@netcom.com | | ||| | |||| | | | || ||
- Boston, MA | 0 | ||| | |||| | | | || ||
- 617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
- C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
- -------------------------------------------------------------------------
-